[% setvar title Immediate subroutines %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Immediate subroutines</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Jean-Louis Leroy
  Date: 4 Aug 2000
  Last Modified: 1 Oct 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 18
  Version: 2
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>This very simple construct, inspired by the Forth language,  makes the
parser extensible by Perl code, providing powerful macro capabilities,
multi-line comments, inline functions and conditional compilation.</p>
<p>CHANGES</p>
<p>Made code examples start at column 4 for proper HTML rendering.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>When the parser sees a subroutine that has been marked as 'immediate',
it calls it immediately. The call's arguments are implicitly quoted as
with q{} and the resulting strings are passed to the subroutine. The
entire call is removed from the parse stream and replaced with the
subroutine's return value.</p>
<a name='SYNTAX'></a><h1>SYNTAX</h1>
<p>use immediate qw( compileif ); # mark subroutines as immediate</p>
<a name='EXAMPLES'></a><h1>EXAMPLES</h1>
<pre>    # multiline comments

    sub comment
    {
        return '';
    }

    use immediate 'comment';

    sub foo
    {       
        # ...
        comment {
            this is a multiline comment;
            the call to comment is executed at parse time
            and returns an empty string that replaces
            the whole call in the parse stream };
    }

    # conditional compilation

    sub compileif
    {
        my ($condition, $body) = @_;
        return eval($condition) ? $body : '';
    }

    use immediate qw( compileif ); # mark subroutines as immediate

    sub bar
    {
        compileif -e 'state'
            {
            do 'state';
            }

        compileif $Module::VERSION &gt; 1.23,
            {
            # blah blah blah
            }
    }

    # macros

    sub square
    {
        my $arg = shift;
        my $gensym = $arg . '_';
        $gensym .= '_' while $arg =~ /$gensym/;
        return &quot;do { $gensym = $arg; $gensym * $gensym }&quot;;
    }</pre>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>A flag is associated with the data structure associated to a
subroutine by the parser. The pragmatic module 'immediate' is used to
turn the flag on. When the parser recognizes a subroutine call, it
checks the flag and if it's true, proceeds as described above.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>The Forth standard.</p>
<p>&quot;Starting Forth&quot;, by Leo Brodie</p>
</div>
